<?php
//select b1.gpname as name,b2.gpname as under from bs_groups as b1,bs_groups as b2 where b1.under=b2.id;
include_once($_SERVER['DOCUMENT_ROOT'].'/bs/include/header.inc');
include_once($_SERVER['DOCUMENT_ROOT'].'/bs/include/comp.inc');

if(isset($_GET['task']))
{
	if(!mysql_connect("localhost",$user,$pass))
		die("Error in Database Connection");

	if(!mysql_select_db("bs"))
		echo("Error in Database Selection");

	if($_GET['task']=="display")
	{
?>
		| <a href="acc_info.php"> Accounts Info</a> | <a href="acc_info.php?info=group">Groups </a> | Display Groups</div>
		<div id="head">
		<div class="right">
		<h3>Secondary Groups</h3><br />
		<table cellpadding="8" border="1" class="main">
		<tr> <th><em>Name</em> </th><th><em>Parent Group</em></th></tr>
<?php		

		$result=mysql_query("select b1.gpname as gpname,b2.gpname as under from bs_groups as b1,bs_groups as b2 where b1.under=b2.id");

		if(!$result)
			echo("Error in Query Execution");
		
		while($arr=mysql_fetch_array($result))
		{
			echo ("<tr><td>{$arr['gpname']}</td><td>{$arr['under']}</td></tr>");
		}
		
		echo "</table>";
		echo"</div>";
?>				
		<div class="left">
		<h3>Primary Groups</h3><br />
		<table cellpadding="8" border="1" class="main">
		<tr> <th><em>Name</em> </th><th><em>Primary Code</em></th></tr>
<?php

		$result=mysql_query("select * from bs_groups where nature <> 'SEC'");

		if(!$result)
			echo("Error in Query Execution");
		
		while($arr=mysql_fetch_array($result))
		{
			echo ("<tr><td>{$arr['gpname']}</td><td>{$arr['nature']}</td></tr>");
		}
		
		echo "</table>";
?>		
		</div>
<?php		
		echo"</div>";

	}
	
	if($_GET['task']=="addform")
	{
?>

		| <a href="acc_info.php"> Accounts Info</a> | <a href="acc_info.php?info=group">Group </a> | Add Group	</div>
		<h3>Add Group</h3>
		<table class="main">
		<form name="addform" action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
		<tr><td><label>Group Name</label> </td><td> <input type="text" name="name" size="21" /></td></tr>
		<tr><td><label>Parent Group</label> </td><td><select name="under">
								<?php 		
								$result=mysql_query("SELECT * FROM `bs_groups` ORDER BY `gpname` ASC LIMIT 0, 30 "); 
//								$result=mysql_query("SELECT * FROM `bs_groups` where under='0' ORDER BY `gpname` ASC LIMIT 0, 30 ");
								if(!$result)
									echo("Error in Query Execution");
								while($arr=mysql_fetch_array($result))
								{
									echo ("<option value='{$arr['id']}'>{$arr['gpname']}</option>");
								}
								?>
								</select>
								 </td></tr>
		<tr><td><input type="submit" value="Add" /></td></tr>
		<input type="hidden" name="task" value="add" />
		</form>
		</table>
			
		
<?php
	}
}
else if(isset($_POST['task']))
{
	if(!mysql_connect("localhost",$user,$pass))
		die("Error in Database Connection");

	if(!mysql_select_db("bs"))
		echo("Error in Database Selection");


	if($_POST['task']=="add")
	{
?>
		| <a href="acc_info.php"> Accounts Info</a> | <a href="acc_info.php?info=group">Group </a> | Add Group	</div>
		<h3>Add Group</h3>
<?php
		$valid=1;
		if(empty($_POST['name']))
		{
			echo "Check Your Input";
			$valid=0;
		}
		else
		{
			$query="select * from bs_groups";
			$result=mysql_query($query);
			while($arr=mysql_fetch_array($result))
			{
				if($_POST['name']==$arr['gpname'])
				{
					$valid=0;
					echo"Duplicate Entry";
					break;
				}
			}
		}
			
			
		if($valid==1)
		{
			$result=mysql_query("insert into bs_groups(id,gpname,under,nature) values(NULL,'{$_POST['name']}','{$_POST['under']}','SEC')");
			if(!$result)
				echo("Error in Query Execution");
			else
				echo "Successful";
				
		}
	}
}




include_once($_SERVER['DOCUMENT_ROOT'].'/bs/include/footer.inc');
?>



